###############################################################################
# Find OPENNI
#
# This sets the following variables:
# OPENNI_FOUND - True if OpenNI was found.
# OPENNI_INCLUDE_DIR - Directories containing the OpenNI include files.
# OPENNI_LIBRARIES - Libraries needed to use OpenNI.
# OPENNI_DEFINITIONS - Compiler flags for OpenNI.

find_package(PkgConfig)
if(${CMAKE_VERSION} VERSION_LESS 2.8.2)
  pkg_check_modules(PC_OPENNI openni-dev)
else()
  pkg_check_modules(PC_OPENNI QUIET openni-dev)
endif()

set(OPENNI_DEFINITIONS ${PC_OPENNI_CFLAGS_OTHER})

#add a hint so that it can find it without the pkg-config
find_path(OPENNI_INCLUDE_DIR_TMP XnStatus.h
          HINTS ${PC_OPENNI_INCLUDEDIR} ${PC_OPENNI_INCLUDE_DIRS} /usr/include/openni /usr/include/ni
          PATHS "$ENV{PROGRAMFILES}/OpenNI/Include" "$ENV{PROGRAMW6432}/OpenNI/Include")

#add a hint so that it can find it without the pkg-config
find_library(OPENNI_LIBRARY
             NAMES OpenNI64 OpenNI
             HINTS ${PC_OPENNI_LIBDIR} ${PC_OPENNI_LIBRARY_DIRS} /usr/lib
             PATHS "$ENV{PROGRAMFILES}/OpenNI/Lib${OPENNI_SUFFIX}" "$ENV{PROGRAMW6432}/OpenNI/Lib${OPENNI_SUFFIX}"
)

set(OPENNI_INCLUDE_DIR ${OPENNI_INCLUDE_DIR_TMP} )
if(APPLE)
  set(OPENNI_LIBRARIES ${OPENNI_LIBRARY} usb)
else()
  set(OPENNI_LIBRARIES ${OPENNI_LIBRARY} )
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(OPENNI DEFAULT_MSG OPENNI_LIBRARIES OPENNI_INCLUDE_DIR)

mark_as_advanced(OPENNI_LIBS OPENNI_INCLUDE_DIR)
if(OPENNI_FOUND)
  include_directories(${OPENNI_INCLUDE_DIR})
endif(OPENNI_FOUND)

